package com.zxl.app.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxl.app.service.SmsService;
import com.zxl.common.Result;
import com.zxl.common.mapper.SysMemberMapper;
import com.zxl.common.model.SmsCfg;
import com.zxl.common.model.bo.sms.SmsCheck;
import com.zxl.common.model.pojo.SysMember;
import com.zxl.common.model.pojo.SysSmsCode;
import com.zxl.common.service.SysSmsCodeService;
import com.zxl.common.utils.RandomUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author zxw
 * @date 2023/4/5
 */
@RestController
@RequestMapping("/app/sms")
@Api(tags = "验证码模块")
public class SmsController {
    @Resource
    private SmsCfg smsCfg;
    @Resource
    private SmsService smsService;
    @Resource
    private SysMemberMapper sysMemberMapper;

    @ApiOperation("手机发送验证码")
    @GetMapping("/send_code")
    public Result SendCode(@RequestParam("phone") @ApiParam(value = "手机号" ,required = true) String phone){
        String code = RandomUtils.getFourBitRandom();
        Map<String, Object> map = new HashMap<>();
        map.put("code",code);
        smsService.sendSms(phone, map, smsCfg.getTemplateCode());
        return Result.success();
    }

    //todo 验证码登录跟微信登录有冲突，会出现两条用户信息
//    @ApiOperation("验证码登录")
//    @PostMapping("/login_code")
//    public Result LoginCode(@RequestBody @Valid SmsCheck smsCheck) {
//        smsService.checkCode(smsCheck);
//        SysMember sysMember = sysMemberMapper.selectOne(new QueryWrapper<SysMember>()
//                .eq("phone", smsCheck.getPhone()));
//        String token = UUID.randomUUID().toString();
//        if (sysMember == null){
//            sysMember.setPhone(smsCheck.getPhone());
//            sysMember.setToken(token);
//            sysMemberMapper.insert(sysMember);
//        }
//        return Result.success(token);
//    }

}
